package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.IFamilyFilesDao;
import org.gxuwz.core.entity.TFamily;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("FamilyFilesDao")
public class FamilyFilesDao extends BaseDao implements IFamilyFilesDao {
	
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}
	
	@Override				//添加家庭档案信息								
	public void addFamilyFilesDao(TFamily files) {							
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(files);
		tx.commit();
		session.close();
	}

	//@Override				//查询家庭档案信息
	@SuppressWarnings("unchecked")
	public List<TFamily> getAll(String houseName, String perscode) {
		List<TFamily> list = null;
		Session session = getSession();
		String hql = "select * from t_family where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(perscode!=null&&houseName!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		if(houseName!=null&&houseName!=""){
			hql += " and houseName like '%"+houseName+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TFamily.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}

	@Override
	public TFamily getByPersCode(String codell) {
		TFamily files = null;
		List<TFamily> list = null;
		Session session = getSession();
		String hql = "select * from t_family where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(codell!=null&&codell!=""){
			hql += " and perscode like '%"+codell+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TFamily.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		
		session.close();
		return list.get(0);
		
/*		Session session = getSession();
		//String hql = "from TFamily where id=?";
		//Query query = session.createQuery(hql);
		String hql = "select * from t_family where perscode=?";
		Query query = session.createSQLQuery(hql).addEntity(TFamily.class);
		query.setParameter(0, codell);
		files = (TFamily) session.get(TFamily.class, codell);
		session.close();
		return files;*/
	}

	@Override
	public void update(TFamily files) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		//session.merge(plan);
		session.update(files);
		tx.commit();
		session.close();
	
		
		
	}

	@Override
	public void delete(TFamily files) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(files);
		tx.commit();
		session.close();
		
	}

}
